-- stealing
-- create by panyl
-- 技能：盗取物品
-- 目前支持如下配置：
-- stealing(rate1,rate2)  rate1为偷窃成功的概率；rate2为盗取高级物品的概率(千分比)

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 目前仅支持盗取怪物的
        if target.type ~= OBJECT_TYPE_MONSTER and target.type ~= OBJECT_TYPE_GHOST_SHIP then
            assert(false);
        end

        -- 如果该怪物已经被盗取过了
        if target.dbase:query("be_stolen") == 1 then
            return;
        end

        local package = MonsterM.query(target.classId, "package") or {};
        local normal = package["normal"];
        local better = package["better"];
        local stealingSucRate   = para[1];

        if package["stealing_suc_rate"] then
            stealingSucRate = package["stealing_suc_rate"];
        end

        -- 没有东西可以盗取
        if not normal and not better then
            return;
        end

        -- 概率偷不到物品
        if DungeonM.getRandSeed("stealing_suc") % 1000 >= stealingSucRate then
            return;
        end

        -- 树屋强盗飞爪触发这个技能
        if ME.user.tree_house
            and source == ME.user.tree_house then
            ME.user.tree_house.dbase:set("stolen", 1);
        end

        local item = normal;
        local rand = DungeonM.getRandSeed("stealing") % 1000;
        local rate = para[2];
        if better and rand < rate then
            -- 高级的
            item = better;
        end

        -- 没有物品？
        if not item then
            return;
        end

        -- 如果有多个，就随机一个
        if type(item[1]) == "table" then
            rand = DungeonM.getRandSeed("stealing") % #item;
            table.sort(item, function(a, b) return a[2] * 1000 + a[3] < b[2] * 1000 + b[3] end);
            item = item[rand + 1];
        end

        -- 标记为被盗过了
        target.dbase:set("be_stolen", 1);

        -- 不再掉落(除了宝箱怪)
        local grid = DungeonM.getGridByPos(target:getPos());
        local keep = true;
        if grid.type ~= GRID_TYPE_BOX and grid.type ~= GRID_TYPE_COFFIN and type(package["stolen_still_drop"]) ~= "number" then
            grid.bonus = nil;
            keep = false;
        end

        -- 奖励就行了
        BonusM.doBonus(item, "stealing");

        -- 抛出被偷窃事件
        EventMgr.fire(event.BE_STOLEN, {
            ["pos"]   = target:getPos(),
            ["type"]  = GRID_TYPE_MONSTER,
            ["bonus"] = { ["bonus"] = item },
            ["remainGrid"] = true,
            ["keep"]  = keep,
            -- ["cost"] = eventCost,
        });

        -- 抛出事件尝试完成：竞技场获得对手物品
        EventMgr.fire(event.GET_ARENA_ENEMY_ITEM, { ["pos"] = target:getPos(), ["bonus"] = item, });

        -- 如果有被盗后变身的，那么变身下
        if package["stolen_transform"] then
            MonsterM.dungeonMonsterTransform(target, package["stolen_transform"], { ["layer"] = target.dbase:query("layer")}, true);

            -- 抛出变身事件
            EventMgr.fire(event.MONSTER_TRANSFORM, {["pos"] = target:getPos(), ["class_id"] = package["stolen_transform"], });
        end

        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
};
